package com.itcast.controller;

import com.itcast.domain.PageBean;
import com.itcast.domain.Student;
import com.itcast.service.Impl.StudentServiceImpl;
import com.itcast.service.StudentService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @param
 * @return
 */
@WebServlet("/StudentServlet/*")
public class StudentServlet extends BeanServlet {
    //在此创建业务层对象，复用
    StudentService service=new StudentServiceImpl();
    public void add(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, ServletException, IOException {
        //1.获取前台提交的表单数据,装进map集合
        Map<String, String[]> map = req.getParameterMap();
        //创建学生对象，准备封装获取 的前端信息
        Student student = new Student();
            //使用工具类的方法，输入map集合和封装类对象自动完成对应赋值
            BeanUtils.populate(student,map);
            //对象封装完成，即可调用业务层的添加方法
            int i=service.add(student);
            //添加完成后，重定向到list方法，因为转发会导致地址栏不变，在刷新页面的时候会造成重复操作
        resp.sendRedirect(req.getContextPath()+"/StudentServlet/findByPage");
    }
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //遍历学生信息，调用业务层遍历方法,结果一个集合对象，即得到的每一条记录封装的学生对象的集合
        List<Student> list=service.findAll();
        //将结果存进请求域中
        req.setAttribute("list",list);
        //转发开始
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id查询，id由修改按钮的提交路径提供
        String id = req.getParameter("id");
        //调用业务层的查询单个方法,返回查询到的一个学生对象
        Student student=service.findById((Integer.parseInt(id)));
        //将学生对象信息塞进域中，返回给页面，以供其回显
        req.setAttribute("student",student);
        req.getRequestDispatcher("/update.jsp").forward(req,resp);
    }
    private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //创建一个分页数据对象
        PageBean<Student> pageBean = new PageBean<>();
        //获取分页数据，即当前页码和每页显示条目数
        String currentPage = req.getParameter("currentPage");
        String rows = req.getParameter("rows");
        //如果当前页码和每页显示的条目数为空或不存在，则给出默认值
        if( "".equals(currentPage) ||currentPage==null  ){
            currentPage="1";
        }
        if( "".equals(rows) ||rows==null  ){
            rows="5";
        }
        //将参数类型转换
        int rows2 = Integer.parseInt(rows);
        int currentPage2 = Integer.parseInt(currentPage);
        //调用业务层方法
        pageBean=service.findByPage(currentPage2,rows2);
        //转发还是重定向呢，都写上
        req.setAttribute("pageBean",pageBean);
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id删除，id由修改按钮的提交路径提供
        String id = req.getParameter("id");
        //调用业务层的查询单个方法,返回查询到的一个学生对象
        int i=service.delete((Integer.parseInt(id)));
        //返回页面
        req.getRequestDispatcher("/StudentServlet/findByPage").forward(req,resp);
    }
    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1.获取前台提交的表单数据,装进map集合
        Map<String, String[]> map = req.getParameterMap();
        //创建学生对象，准备封装获取 的前端信息
        Student student = new Student();
        //使用工具类的方法，输入map集合和封装类对象自动完成对应赋值
        BeanUtils.populate(student,map);
        int i=service.update(student);
        //重定向到主界面
        resp.sendRedirect(req.getContextPath()+"/StudentServlet/findByPage");
    }
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //获取请求参数
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        System.out.println("username = " + username);
        String password = req.getParameter("password");
        String ck = req.getParameter("ck");
        //调用业务层方法，接受查询结果
        boolean flag = service.login(username, password);
        if(flag){//查到了
            if("1".equals(ck)){//并且选择了记住操作
                //将登录信息值存进session
                HttpSession session = req.getSession();
                session.setAttribute("username",username);
                session.setAttribute("password",password);
            }
            //将转发到
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }else {
            //查询不到，登录失败，转发到登录页面
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }
    }
}
